import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/class/ClassResp.dart';
import 'package:ichinesereader/pages/common/report/reportsStudentPersonalContent.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/appTheme.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonsParentSwitchChild.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerParent.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/parentSidebar.dart';
import '../../themes/pageStyles.dart';

class ParentReport extends StatefulWidget {
  static const String routeName = '/parent/reports';

  @override
  _ParentReportState createState() => _ParentReportState();
}

class _ParentReportState extends State<ParentReport> {
  final UserController _userController = Get.find();
  final ReportsController _reportsController = Get.put(ReportsController());
  final box = Hive.box('appconfig');
  var _childUserName = '';

  @override
  void initState() {
    bool isParent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Parent;
    if (isParent) {
      if (_userController.parentChildren.isEmpty) {
        Future.delayed(Duration(milliseconds: 200), (() {
          _userController.getProfile();
        }));
      } else {
        _childUserName = _userController.currentChild.value.userName!;
        Future.delayed(Duration(milliseconds: 100), (() {
          if(_childUserName!='')
          _userController.getParentClassOptions(_childUserName);
        }));
        
      }
    }
    super.initState();
  }

  @override
  void dispose() {
    
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('start building parent report page');
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.parentPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();
    _childUserName = _userController.currentChild.value.userName!;

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Reports'.tr) : null,
      drawer: isSmallScreen ? ParentAppDrawer() : null,
      floatingActionButton: FloatingActionButtonsParentSwitchChild(),
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Visibility(
              visible: !isSmallScreen,
              child: ParentSidebarX(
                  selectedIndex: _selectedSidebarItem,
                  isBigScreen: isBigScreen),
            ),
            Expanded(
                child: Container(
                    margin: _pageStyles.mainContainerPageEdgeInsets(),
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Container(
                              padding: EdgeInsets.only(left: 30.0),
                              margin: EdgeInsets.only(bottom: 10.0),
                              decoration: BoxDecoration(
                                  color: Get.isDarkMode
                                      ? Colors.black
                                      : Colors.black.withOpacity(0.1),
                                  borderRadius: BorderRadius.circular(6.0)),
                              child: Row(children: [
                                Obx(() {
                                  if (_userController
                                      .parentClassOptions.isNotEmpty) {
                                    return DropdownButton(
                                        dropdownColor: Get.isDarkMode
                                            ? AppTheme.colors.darkTheme4
                                            : Get.theme.primaryColor,
                                        icon: Icon(
                                          Icons.arrow_drop_down_outlined,
                                          color: Get.isDarkMode
                                              ? Get.theme.primaryColorDark
                                              : Colors.black,
                                        ),
                                        value: _userController
                                            .parentSelctedClass.value,
                                        items: _userController
                                            .parentClassOptions
                                            .map((value) {
                                          return DropdownMenuItem(
                                            value: value,
                                            child: Text(
                                              value.className.toString(),
                                              style: TextStyle(
                                                  fontSize: 16.0,
                                                  color: Get.isDarkMode
                                                      ? Get.theme
                                                          .primaryColorDark
                                                      : Get.theme
                                                          .primaryColorLight),
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                          );
                                        }).toList(),
                                        onChanged: (value) {
                                          value as ClassResp;

                                          _userController
                                              .updateParentSelectedClass(value);
                                          String _classId =
                                              value.classId.toString();
                                          _reportsController
                                              .refetchStudentPersonalReport(
                                                  _classId, _childUserName);
                                        });
                                  } else {
                                    return Text('');
                                  }
                                })
                              ])),
                          Expanded(child: Obx(() {
                            if (_userController
                                    .parentSelctedClass.value.classId !=
                                null) {
                              return ReportStudentPersonalContent(
                                userName: _userController
                                    .currentChild.value.userName!,
                                classId: _userController
                                    .parentSelctedClass.value.classId!,
                              );
                            } else {
                              return Text('');
                            }
                          }))
                        ])))
          ],
        ),
      ),
    );
  }
}
